package com.kelvem.saas.workbanch.core.manager.controller;

import com.kelvem.saas.workbanch.core.base.Page;
import com.kelvem.saas.workbanch.core.exception.SaasAssert;
import com.kelvem.saas.workbanch.core.manager.model.MetaTableActionEntity;
import com.kelvem.saas.workbanch.core.manager.request.MetaTableActionQueryVo;
import com.kelvem.saas.workbanch.core.manager.service.MetaTableActionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 实体表行为控制层
 *
 * @author kelvem
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/metaTableAction")
public class MetaTableActionController {

    @Resource
    private MetaTableActionService metaTableActionService;
    
    /**
     * 增加实体表行为
     * curl -X POST http://localhost:9090/metaTableAction/v1/add -H 'Content-Type: application/json' -d '{"id": null, "metaTableId": 0, "templatePositionType": "template_position_type", "sortNo": 6, "eventName": "event_name", "templateType": "template_type", "eventSourceType": "event_source_type", "eventTriggerType": "event_trigger_type", "scopeType": "event_param_type", "relatedType": "related_type", "relatedId": 1, "relatedUrl": "related_url", "relatedParams": "related_params", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTableAction 实体表行为信息
     * @return Long 主键id
     */
    @PostMapping("/v1/add")
    public Long add(@RequestBody(required = false) MetaTableActionEntity metaTableAction) {
        SaasAssert.notNull(metaTableAction);

        log.debug("MetaTableActionController:add:start, metaTableAction=【{}】", metaTableAction);
        Long metaTableActionId = metaTableActionService.addMetaTableAction(metaTableAction);
        log.debug("MetaTableActionController:add:end, metaTableActionId={}", metaTableActionId);

        return metaTableActionId;
    }

    /**
     * 修改实体表行为
     * curl -X PUT http://localhost:9090/metaTableAction/v1/update -H 'Content-Type: application/json' -d '{"id": 10000, "metaTableId": 0, "templatePositionType": "template_position_type", "sortNo": 6, "eventName": "event_name", "templateType": "template_type", "eventSourceType": "event_source_type", "eventTriggerType": "event_trigger_type", "scopeType": "event_param_type", "relatedType": "related_type", "relatedId": 1, "relatedUrl": "related_url", "relatedParams": "related_params", "createUid": "create_uid", "updateUid": "update_uid", "memo": "memo"}'
     *
     * @param metaTableAction 实体表行为信息
     * @return Integer 修改记录数
     */
    @PostMapping("/v1/update")
    public Integer update(@RequestBody(required = false) MetaTableActionEntity metaTableAction) {
        SaasAssert.notNull(metaTableAction);

        log.debug("MetaTableActionController:update:start, metaTableAction=【{}】", metaTableAction);
        int count = metaTableActionService.updateMetaTableAction(metaTableAction);
        log.debug("MetaTableActionController:update:end, count={}", count);

        return count;
    }

    /**
     * 删除实体表行为
     * curl -X DELETE 'http://localhost:9090/metaTableAction/v1/delete/10002'
     *
     * @param metaTableActionId 实体表行为id
     * @return Integer 删除记录数
     */
    @PostMapping("/v1/delete/{metaTableActionId}")
    public Integer delete(@PathVariable(required = false) Long metaTableActionId) {
        SaasAssert.notNull(metaTableActionId);

        log.debug("MetaTableActionController:delete:start, metaTableActionId={}", metaTableActionId);
        int count = metaTableActionService.deleteMetaTableAction(metaTableActionId);
        log.debug("MetaTableActionController:delete:end, count={}", count);

        return count;
    }

    /**
     * 查询实体表行为分页数据
     * curl -X POST http://localhost:9090/metaTableAction/v1/queryPage -H 'Content-Type: application/json' -d '{"id": 10000}'
     *
     * @param metaTableActionQueryVo 查询用
     * @return Page<MetaTableActionEntity> 实体表行为分页数据
     */
    @PostMapping("/v1/queryPage")
    public Page<MetaTableActionEntity> queryPage(@RequestBody(required = false) MetaTableActionQueryVo metaTableActionQueryVo) {
        SaasAssert.notNull(metaTableActionQueryVo);

        log.debug("MetaTableActionController:queryPage:start, metaTableActionQueryVo=【{}】", metaTableActionQueryVo);
        Page<MetaTableActionEntity> page = metaTableActionService.queryMetaTableActionPage(metaTableActionQueryVo);
        log.debug("MetaTableActionController:queryPage:end, page.count={}", page.getSize());

        return page;
    }

    /**
     * 根据id查实体表行为信息
     * curl -X GET 'http://localhost:9090/metaTableAction/v1/get/10000'
     *
     * @param metaTableActionId 实体表行为id
     * @return MetaTableAction 实体表行为信息
     */
    @GetMapping("/v1/get/{metaTableActionId}")
    public MetaTableActionEntity getById(@PathVariable(required = false) Long metaTableActionId) {
        SaasAssert.notNull(metaTableActionId);

        log.debug("MetaTableActionController:getById:start, metaTableActionId={}", metaTableActionId);
        MetaTableActionEntity metaTableAction = metaTableActionService.getMetaTableActionById(metaTableActionId);
        log.debug("MetaTableActionController:getById:end, metaTableAction=【{}】", metaTableAction);

        return metaTableAction;
    }
    
}
